// Copyright (c) 2010 Marcos David Marín Amador <marcosmarin@gmail.com>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

function cfaqs_quote(boardwrap) {
	var quote_button_text = "quote";
	
	function setup_quote(author, body) {
		// strip sig
		var sig_start = body.indexOf("---");
		if (sig_start != -1) {
			body = body.substring(0, sig_start);
		}
		
		// strip invalid html tags
		// TODO: Might need more tags here, these are the ones I thnk are most lightly
		// to be added by other addons like ClickableLinks
		body = body.replace(/<\/?(span|div|a|img)[^>]*>/ig, "");
		
		// removed quoted text
		body = body.replace(/<i><p><strong>From.*<\/strong>.*<\/p><\/i>/, "<i>[quoted text]</i>\n");
		
		// strip <br>s
		var old_body = "";
		while (old_body != body) {
			old_body = body;
			body = body.replace("<br>", "\n");
		}

		// trim end of newlines
		var strend = body.length - 1;
		while (body[strend] == '\n') {
			strend--;
		}
		body = body.substring(0, strend + 1);
			
		var text = document.getElementById("cfaqs-quickpost-text");
		var before_text = text.value.substring(0, text.selectionStart);
		var after_text = text.value.substring(text.selectionEnd, text.value.length);
		var quoted_text = "<i><p><strong>From: " + author + "</strong>\n" + body + "</p></i>\n"
		text.value = before_text + quoted_text + after_text;
		text.focus();
		
		var caret_position = before_text.length + quoted_text.length;
		text.setSelectionRange(caret_position, caret_position);
		
		location.hash = "cfaqs-quickpost-form";
	}
	
	// quote when msg_stats is on the left
	function quote_stats_left(messages, quote_link) {
		function quote_click(target) {
			var msg = target.parentNode.parentNode.parentNode;
			var msg_body = msg.lastChild.firstChild.innerHTML;
			var msg_author = msg.firstChild.innerText;
			
			msg_author = msg_author.substring(0, msg_author.indexOf("\n"));
			
			setup_quote(msg_author, msg_body);
		}
		
		for (var i = 0; i < messages.length; i++) {
			var msg = messages[i];
			
			var msg_stats = $('div.msg_stats_left', msg)[0];
			
			var link = quote_link.cloneNode(true);
			link.onclick = function(event) {
				quote_click(event.target);
			}
			
			msg_stats.appendChild(document.createElement("br"));
			msg_stats.appendChild(link);
		}
	}
	
	// quote when msg_stats is on top
	function quote_stats_top(messages, quote_link) {
		function quote_click(target) {
			// link -> div -> td -> tr -> next tr -> td -> div -> text
			var msg_body = target.parentNode
				.parentNode.parentNode
				.nextSibling.firstChild
				.firstChild.innerHTML;
			
			// link -> div -> text
			var msg_author = target.parentNode.innerText;
			
			msg_author = msg_author.substring(0, msg_author.indexOf(" "));
			
			setup_quote(msg_author, msg_body);
		}
		
		for (var i = 0; i < messages.length; i++) {
			var msg = messages[i];
			var div = msg.firstChild.firstChild;
			
			if (div.className == "msg_stats") {
				var link = quote_link.cloneNode(true);
				link.onclick = function(event) {
					quote_click(event.target);
				}
				
				div.appendChild(document.createTextNode(" | "));
				div.appendChild(link);
			}
		}
	}
	
	var tbody = boardwrap.getElementsByTagName("tbody")[0];
	var messages = tbody.childNodes;
	
	var quote_link = document.createElement("span");
	quote_link.innerText = quote_button_text;
	quote_link.className = "cfaqs-quote-link";
	
	if (messages[0].childNodes.length == 1) {
		quote_stats_top(messages, quote_link);
	} else {
		quote_stats_left(messages, quote_link);
	}
}